home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / misc / prtfolio / sft.arj / IN_BR.I < prev    next >
Text File  |  1991-03-20  |  5KB  |  185 lines

  1. /*
  2. Conversion of "C" source programs from "indent"-version to "bracket"-version
  3. indent-version: brackets, which are necessary to mark program blocks
  4.                 are expressed by brackets, like in OCCAM and PROMAL, advantage
  5.                 of this method: programs become shorter and better readable
  6. bracket-vers.:  normal method of C-syntax to mark program blocks: brackets
  7.  
  8. 06.03.91:       extended to support ansii's double-slash-comment
  9.  
  10. Rolf Henze
  11. Dorfstrasse 19
  12. D-3300 Braunschweig
  13. W.-Germany
  14. phone: 0049-531-509839
  15.  
  16. sep 1990
  17. last edit: 20.03.91
  18. */
  19.  
  20. //---------------------------------------------------------------------------
  21.  
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25.  
  26. #define true  1
  27. #define false 0
  28.  
  29. #define linelen   256
  30. unsigned char line[linelen];
  31. unsigned char scratch[linelen];
  32. int indent;
  33. int i;
  34. int errflg;
  35. int stmflg;         //true after 'comment-on', false after 'comment-off
  36. int linecount;
  37.  
  38. //---------------------------------------------------------------------------
  39.  
  40. // test if line is statement or comment
  41.  
  42. int statement(str)
  43. unsigned char *str;
  44.  
  45.   int i;
  46.   int flag;
  47.  
  48.   if (strlen(str)>0)                 /* if str is not empty*/
  49.     i=0;
  50.     while (str[i]==0x20)             /* skip leading blanks */
  51.       i++;
  52.     if (str[i]!=0)
  53.       if (stmflg)
  54.         if (*(str+i) != '/')
  55.           flag = true;               /* statement */
  56.         else
  57.           if(*(str+i+1) == '*')
  58.             flag = false;            /* kommentar */
  59.       else
  60.         flag = false;
  61.   
  62.       while (*(str+i) != 0)
  63.         if (*(str+i)=='/' && *(str+i+1)=='*')
  64.           stmflg = false;
  65.         else
  66.           if (*(str+i)=='*' && *(str+i+1)=='/')
  67.             stmflg = true;
  68.         i++;
  69.  
  70.     else
  71.       flag = false;                  /* str enthaelt nur blanks */
  72.   
  73.   else
  74.     flag = false;                    /* str leer */
  75.  
  76.   return flag;
  77.  
  78. //---------------------------------------------------------------------------
  79.  
  80.  
  81. int indent_level(str)
  82. unsigned char *str;
  83.  
  84.   int i;
  85.   /* calculates indent-level of*/
  86.   /* PROMAL program line*/
  87.  
  88.   i=0;
  89.   while (str[i]==0x20)
  90.     i++;
  91.   if (str[i]!=0)
  92.     if (i%2 != 0)
  93.       errflg = true;
  94.     return  i>>1;
  95.   else
  96.     return 0;
  97.  
  98. //---------------------------------------------------------------------------
  99.  
  100. int indentation(level)
  101. int level;
  102.  
  103.   int i;
  104.   int k;
  105.   int l;
  106.  
  107.   l=indent_level(line);                  /* determine indent-level of input line*/
  108.   if ((l-level) > 1)
  109.     errflg = true;
  110.  
  111.   if (l!=level)                         /* insert bracket, if indent-level*/
  112.     if (l>level)                        /* has changed*/
  113.       i=0;
  114.       if (l>0)                          /* insert blanks for indentation*/
  115.         while (i<2*l)
  116.           scratch[i] = 0x20;
  117.           i++;
  118.       scratch[i] = 0x7b;
  119.       i++;
  120.       scratch[i] = 0;
  121.       puts(scratch);                   /* open bracket, and new line*/
  122.     else
  123.       k = level-1;
  124.       while (k>=l)
  125.         i=0;
  126.         if (k>-1)
  127.           while (i < 2*(k+1))
  128.             scratch[i] = 0x20;
  129.             i++;
  130.         scratch[i] = 0x7d;
  131.         i++;
  132.         scratch[i] = 0;
  133.         puts(scratch);                 /* close bracket*/
  134.         k--;
  135.   
  136.   return l;
  137.  
  138. //---------------------------------------------------------------------------
  139.  
  140. void ext_cmt(str)       /* extend ansii's double-slash-comment */
  141. unsigned char *str;
  142.  
  143.   int i;
  144.   int len;
  145.  
  146.   len = strlen(str);
  147.   for (i=0; i<len; i++)
  148.     if (*(str+i)=='/' && *(str+i+1)=='/')
  149.       *(str+i+1) = '*';
  150.       strcpy(str+len,"*/");
  151.       break;
  152.  
  153. //---------------------------------------------------------------------------
  154.  
  155. void main()
  156.  
  157.   errflg = false;
  158.   stmflg = true;
  159.   linecount = 0;
  160.   indent = 0;
  161.   
  162.   while (gets(line)!=NULL)              /* read until EOF is detected*/
  163.     linecount++;
  164.     ext_cmt(line);                      /* extend ansii's double-slash-comment*/
  165.     if (statement(line))                /* if line is not empty or commnt*/
  166.       indent = indentation(indent);     /* determine id-level, set bracket*/
  167.     puts(line);
  168.     if (errflg != false)                /* stop, if indentation error! */
  169.       fprintf(stderr,"\nindentation error line %d",linecount);
  170.       exit(1);
  171.  
  172.   while (indent > 0)                  /* close open brackets, before leaving */
  173.     for (i=0; i<(indent<<1); i++)
  174.       scratch[i] = 0x20;
  175.     scratch[i] = 0x7d;
  176.     i++;
  177.     scratch[i] = 0;
  178.     puts(scratch);
  179.     indent--;
  180.  
  181.   exit(0);
  182.   
  183. //--------------------- end of file in_br.i ---------------------------------
  184.  
  185.